
const imageSetting = (url,width,height) => {
    var nUrl = url + '?x-oss-process=image/resize,m_lfit,'
    if (width > 0){
        nUrl += ('w_'+width + ',')
    }
    if (height > 0){
        nUrl += ('h_'+height)
    }
    return nUrl
}

// 1. 时间戳 转时间
const dateYMDformat = value => {
    var date = new Date()
    date.setTime(value)
    var month = date.getMonth() + 1
    var hours = date.getHours()
    if (hours < 10)
	    hours = '0' + hours
    var minutes = date.getMinutes()
    if (minutes < 10)
	    minutes = '0' + minutes
		
	var mainMonth = month	
	if (month < 10)	{
		mainMonth = '0' + month
	}
	var day1 = date.getDate();
	var mainDay = day1;
	if (day1 < 10){
		mainDay = '0' + day1;
	}
		
    var time = date.getFullYear() + '-' + mainMonth + '-' + mainDay;
    return time
}
// 处理ios设备时间格式
const parsePlatformTime = (time) => {
	if (!time) {
		return ''
	}
	const platform = uni.getSystemInfoSync().platform
	let newTime = ''
	if (platform === 'ios') {
		newTime = parseTime(time, '{y}/{m}/{d} {h}:{i}:{s}')
	} else if (platform === 'android') {
		newTime = parseTime(time, '{y}-{m}-{d} {h}:{i}:{s}')
	} else {
		if (uni.getSystemInfoSync().deviceModel.includes('iPhone')) {
			newTime = parseTime(time, '{y}/{m}/{d} {h}:{i}:{s}')
		} else {
			newTime = parseTime(time, '{y}-{m}-{d} {h}:{i}:{s}')
		}
	}
	return newTime
}

// 日期格式化
const parseTime = (time, pattern) => {
    if (arguments.length === 0 || !time) {
        return null
    }
    const format = pattern || '{y}-{m}-{d} {h}:{i}:{s}'
    let date
    if (typeof time === 'object') {
        date = time
    } else {
        if ((typeof time === 'string') && (/^[0-9]+$/.test(time))) {
            time = parseInt(time)
        } else if (typeof time === 'string') {
            time = time.replace(new RegExp(/-/gm), '/').replace('T', ' ').replace(new RegExp(/\.[\d]{3}/gm), '')
        }
        if ((typeof time === 'number') && (time.toString().length === 10)) {
            time = time * 1000
        }
        date = new Date(time)
    }
    const formatObj = {
        y: date.getFullYear(),
        m: date.getMonth() + 1,
        d: date.getDate(),
        h: date.getHours(),
        i: date.getMinutes(),
        s: date.getSeconds(),
        a: date.getDay()
    }
    const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
        let value = formatObj[key]
        // Note: getDay() returns 0 on Sunday
        if (key === 'a') { return ['日', '一', '二', '三', '四', '五', '六'][value] }
        if (result.length > 0 && value < 10) {
            value = '0' + value
        }
        return value || 0
    })
    return time_str
}
const parseTimeWithWeek = time => {    var mainTime = ''    if (uni.getDeviceInfo().deviceBrand === 'iphone'){        mainTime = time.replace(/-/g,'/')    } else {        mainTime = time    }    var info = new Date(mainTime).getDay()    if (info === 1){        return '周一'    } else if (info === 2){        return '周二'    } else if (info === 3){	 	 return '周三'    } else if (info === 4){        return '周四'    } else if (info === 5){        return '周五'    } else if (info === 6){        return '周六'    } else if (info === 0){        return '周日'    }}/**
 * 获取本周、本季度、本月、上月的开始日期、结束日期
 */
var now = new Date() // 当前日期
var nowDayOfWeek = now.getDay() // 今天本周的第几天
var nowDay = now.getDate() // 当前日
var nowMonth = now.getMonth() // 当前月
var nowYear = now.getYear() // 当前年
nowYear += (nowYear < 2000) ? 1900 : 0 //
var lastMonthDate = new Date() // 上月日期
lastMonthDate.setDate(1)
lastMonthDate.setMonth(lastMonthDate.getMonth() - 1)
var lastYear = lastMonthDate.getYear()
var lastMonth = lastMonthDate.getMonth()

// 格式化日期：yyyy-MM-dd
const formatDate = (date,type) => {    if(!date){        return    }    var myyear = date.getFullYear()
    var mymonth = date.getMonth() + 1
    var myweekday = date.getDate()
    if (mymonth < 10) {
        mymonth = '0' + mymonth
    }
    if (myweekday < 10) {
        myweekday = '0' + myweekday
    }
        var mainBackInfo = ''    if (type === 1){        mainBackInfo = ( myyear + '-' + mymonth + '-' + myweekday)    } else {        mainBackInfo = ( mymonth + '-' + myweekday)    }	    var mainTime = ''    if (uni.getDeviceInfo().deviceBrand === 'iphone'){	    mainTime = mainBackInfo.replace(/-/g,'/')    } else {	    mainTime = mainBackInfo    }    return mainTime 	
}const getWeekDayRange = index => {	  // 1.本周起始	  let day = nowDayOfWeek	  let weekDay = 0	  let weekDay1= 0	  if (!day) {	    day = 7	  }	  weekDay = index * 7 	  weekDay1= index * 7 + 1 	  var weekStartDate = new Date(nowYear, nowMonth, nowDay - (day - 1) + weekDay)	  	  // 2.本周结束	   var weekEndDate = new Date(nowYear, nowMonth, nowDay + (6 - nowDayOfWeek) + weekDay1)	  return (formatDate(weekStartDate) +' - ' + formatDate(weekEndDate))}const getWeekDayRangeStart = index => {	  // 1.本周起始	  let day = nowDayOfWeek	  let weekDay = 0	  let weekDay1= 0	  if (!day) {	    day = 7	  }	  weekDay = index * 7 	  weekDay1= index * 7 + 1 	  var weekStartDate = new Date(nowYear, nowMonth, nowDay - (day - 1) + weekDay)	  	  // 2.本周结束	   var weekEndDate = new Date(nowYear, nowMonth, nowDay + (6 - nowDayOfWeek) + weekDay1)	  return formatDate(weekStartDate,1) }const getWeekDayRangeEnd = index => {	  // 1.本周起始	  let day = nowDayOfWeek	  let weekDay = 0	  let weekDay1= 0	  if (!day) {	    day = 7	  }	  weekDay = index * 7 	  weekDay1= index * 7 + 1 	  	  // 2.本周结束	   var weekEndDate = new Date(nowYear, nowMonth, nowDay + (6 - nowDayOfWeek) + weekDay1)	  return formatDate(weekEndDate,1) }

const getWeekDayRangeEndFormat = time => {
	
	var mainTime = '';
		  	  if (uni.getDeviceInfo().deviceBrand === 'iphone'){
		  console.log('我是iphone',time);
		  var infoStr = time.replace(/-/g,'/')
		  if (time.indexOf('/') !== -1){
			var infoList = time.split('/');
			
			infoList.forEach((item, index) => {
				if (index === infoList.length - 1){
					mainTime = mainTime + item;
				} else {
					mainTime = mainTime + item + '-';
				}
			});
		  } else {
			  mainTime = time;
		  }
	  } else {
	      mainTime = time
	  }
	  return mainTime;}

function formatTimeTwo(number, format) {

  var formateArr = ['Y', 'M', 'D', 'h', 'm', 's'];
  var returnArr = [];

  var date = new Date(number * 1000);
  returnArr.push(date.getFullYear());
  returnArr.push(formatNumber(date.getMonth() + 1));
  returnArr.push(formatNumber(date.getDate()));

  returnArr.push(formatNumber(date.getHours()));
  returnArr.push(formatNumber(date.getMinutes()));
  returnArr.push(formatNumber(date.getSeconds()));

  for (var i in returnArr) {
      format = format.replace(formateArr[i], returnArr[i]);
  }
  return format;
}
const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}


// 6.退出登录
const memberLogoutManager=()=>{
    console.log('退出登录=======')
    var that = this
    return new Promise (resolve => {
			// 1. 清空用户access_token
			uni.removeStorageSync('access_token')
			// 4. 清空openid
			uni.removeStorageSync('openid')
			// 5. 清空sessionKey
			uni.removeStorageSync('sessionKey')
			
			uni.removeStorageSync('auth')
					
			// 4. 清空当前用户信息
			if (getApp().globalData ){
				getApp().globalData = {
					userInfo: null,
					userId: null
				}
			}
			resolve()
    })	
}


const utils = {
    imageSetting,
    dateYMDformat ,    parseTime,    parseTimeWithWeek,    getWeekDayRange,    getWeekDayRangeEnd,    getWeekDayRangeStart,
		getWeekDayRangeEndFormat,
		formatTimeTwo,
		memberLogoutManager,
		formatDate,
		parsePlatformTime
}
module.exports = utils